﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EjemploSVN.Models;
using System.Data.Entity;

namespace EjemploSVN.DataAccess
{
    public abstract class BaseRepo<T> where T: Entidad
    {
        protected ModelContext DbContext {
            get { return (this.uowFactory.Current as DefaultUnitOfWork).DbContext; }
        }

        private IUnitOfWorkFactory uowFactory;

        public abstract DbSet<T> Set
        {
            get;
        }

        public BaseRepo(IUnitOfWorkFactory uowFactory)
        {
            this.uowFactory = uowFactory;
        }

        public T Get(int id)
        {
            return this.Set.Find(id);
        }

        public T Add(T student)
        {
            return this.Set.Add(student);
        }

        public void Update(T student)
        {
            this.DbContext.Entry(student).State = System.Data.EntityState.Modified;
        }

        public void Remove(T student)
        {
            this.Set.Remove(student);
        }

        public IList<T> GetAll()
        {
            return this.Set.ToList();
        }

    }
}